package com.zy.openchat.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.View;

import com.ja.openchat.R;
import com.zy.openchat.Constants;
import com.zy.openchat.core.model.user.UserInfo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ScreenViewUtil {

    public static Bitmap getMagicDrawingCache(Context context, View view, boolean quick_cache) {
        Bitmap bitmap = (Bitmap) view.getTag(R.id.cacheBitmapKey);
        Boolean dirty = (Boolean) view.getTag(R.id.cacheBitmapDirtyKey);
        if (view.getWidth() + view.getHeight() == 0) {
            view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        }
        int viewWidth = view.getWidth();
        int viewHeight = view.getHeight();
        if (bitmap == null || bitmap.getWidth() != viewWidth || bitmap.getHeight() != viewHeight) {
            if (bitmap != null && !bitmap.isRecycled()) {
                bitmap.recycle();
            }
            bitmap = Bitmap.createBitmap(viewWidth, viewHeight, Bitmap.Config.ARGB_8888);
            view.setTag(R.id.cacheBitmapKey, bitmap);
            dirty = true;
        }
        if (dirty || !quick_cache) {
            bitmap.eraseColor(context.getResources().getColor(android.R.color.white));
            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);
            view.setTag(R.id.cacheBitmapDirtyKey, false);
        }
        return bitmap;
    }


    public static String getFileByView(Context context, View view) {
        String filePath = Constants.userImagePath();
        String fileName = "qr_" + UserInfo.getUserInfo().id + ".jpg";
//        File targetFile = new File(filePath,filePath);
//        if(targetFile.exists()){
//            return targetFile.getAbsolutePath();
//        }
        return saveFile(getMagicDrawingCache(context, view, true), filePath, fileName);
    }

    private static String saveFile(Bitmap bm, String filePath, String fileName) {
        boolean flag = false;
        File pathFile = new File(filePath);
        if (!pathFile.exists()) {
            pathFile.mkdirs();
        }
        File myCaptureFile = new File(pathFile, fileName);
        FileOutputStream bos = null;
        try {
            bos = new FileOutputStream(myCaptureFile);
            flag = bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return myCaptureFile.getAbsolutePath();
    }

    public interface OnScreenshotCallback {
        void onSuccess(String filePath);

        void onError();
    }

}
